package com.listener;

import com.entity.Type;
import com.service.TypeService;
import com.service.impl.TypeServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

@WebListener
public class TypeServletContextListener implements ServletContextListener {
    //对象创建
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
            //1、获取当前数据库中的所有信息
        TypeService typeService = new TypeServiceImpl();
        List<Type> types = typeService.getAll();
        //2、获取application对象
        ServletContext application = servletContextEvent.getServletContext();

        //3、将信息存在application
        application.setAttribute("types",types);
    }
//对象销毁
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
